package com.coscon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.coscon.mapper.WakeUpConfigMapper;
import com.coscon.model.db.WakeUpConfig;
import com.coscon.service.WakeUpService;
import com.coscon.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class WakeUpServiceImpl implements WakeUpService {

    @Resource
    private WakeUpConfigMapper wakeUpConfigMapper;


    @Override
    public boolean saveWakeUpConfig(String mac, String hostname) {
        boolean f = false;
        if(StringUtils.isBlank(mac)){
            return f;
        }
        WakeUpConfig wakeUpConfigTemp = this.getWakeUpConfigByMac(mac);
        if(wakeUpConfigTemp!=null){
            return f;
        }
        if(StringUtils.isBlank(hostname)){
            return f;
        }
        mac = StringUtils.replace(mac,":","-");
        mac = StringUtils.replace(mac,",","-");
        mac = StringUtils.replace(mac," ","-");
        WakeUpConfig wakeUpConfig = new WakeUpConfig();
        wakeUpConfig.setAddDateTime(DateUtils.nowStr());
        wakeUpConfig.setMac(mac);
        wakeUpConfig.setHostName(hostname);
        f = this.wakeUpConfigMapper.insert(wakeUpConfig)>0;
        return f;
    }

    @Override
    public WakeUpConfig getWakeUpConfigByMac(String mac) {
        WakeUpConfig wakeUpConfig = null;
        if(StringUtils.isBlank(mac)){
            return wakeUpConfig;
        }
        QueryWrapper<WakeUpConfig> ew = new QueryWrapper();
        ew.eq("MAC",mac);
        List<WakeUpConfig> list = this.wakeUpConfigMapper.selectList(ew);
        if(list!=null && !list.isEmpty()){
            wakeUpConfig = list.get(0);
        }
        return wakeUpConfig;
    }

    @Override
    public List<WakeUpConfig> getWakeUpConfigList() {
        QueryWrapper<WakeUpConfig> ew = new QueryWrapper();
        ew.orderByDesc("LAST_UPDATE_TIME");
        return this.wakeUpConfigMapper.selectList(ew);
    }

    @Override
    public boolean deleteById(String dataId) {
        UpdateWrapper<WakeUpConfig> eq = new UpdateWrapper<>();
        eq.eq("DATA_ID",dataId);
        return this.wakeUpConfigMapper.delete(eq)>0;
    }



}
